package com.jspx.txweb.action;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.OAuthOpenIdType;
import com.jspx.boot.sign.SexType;
import com.jspx.json.JSONObject;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.env.TXWebIoc;
import com.jspx.txweb.online.OnlineManager;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.table.OAuthOpenId;
import com.jspx.txweb.util.RequestUtil;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@HttpMethod(caption = "QQ登陆接口")
public class QqAfterLoginRedirectAction extends ActionSupport {
    private static final Logger log = LoggerFactory.getLogger(QqAfterLoginRedirectAction.class);
    protected OnlineManager onlineManager;
    @Ref(name = TXWebIoc.onlineManager)
    public void setOnlineManager(OnlineManager onlineManager) {
        this.onlineManager = onlineManager;
    }


    @Override
    public String execute() throws Exception {

        try {
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            String accessToken = null,
             openID = null;
            long tokenExpireIn = 0L;


            if (accessTokenObj.getAccessToken().equals("")) {
//                我们的网站被CSRF攻击了或者用户取消了授权
//                做一些数据统计工作
                printError("没有获取到响应参数",403);
                return NONE;
            } else {

                accessToken = accessTokenObj.getAccessToken();
                tokenExpireIn = accessTokenObj.getExpireIn();

                session.setAttribute("qq_tokenExpireIn",String.valueOf(tokenExpireIn));
                //  request.getSession().setAttribute("demo_access_token", accessToken);
                //  request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));

                // 利用获取到的accessToken 去获取当前用的openid -------- start
                OpenID openIDObj = new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();

                // 利用获取到的accessToken 去获取当前用户的openid --------- end
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
                if (userInfoBean.getRet() == 0) {
                    OAuthOpenId newOAuthOpenId = new OAuthOpenId();
                    newOAuthOpenId.setName(userInfoBean.getNickname());
                    newOAuthOpenId.setSex(SexType.getSexZh(SexType.getSex(userInfoBean.getGender())));
                    newOAuthOpenId.setOpenId(openID);
                    newOAuthOpenId.setUnionid("");
                    newOAuthOpenId.setFaceImage(userInfoBean.getAvatar().getAvatarURL100());
                    newOAuthOpenId.setNamespace(OAuthOpenIdType.qq);
                    newOAuthOpenId.setAccessToken(accessToken);
                    JSONObject jsonobject = null;// todo  onlineManager.openIdLogin(newOAuthOpenId, RequestUtil.getRemoteAddr(request),false);
                    if (jsonobject.getBoolean(Environment.SUCCESS))
                    {
                        return SUCCESS;
                    } else {
                        printError(jsonobject.getString(Environment.message),  403);
                    }
                } else {
                    printError("很抱歉，我们没能正确获取到您的信息，原因是： " + userInfoBean.getMsg(), 403);
                }
            }
        } catch (QQConnectException e) {
            log.error("QQ登陆验证失败",e);
        }
        return NONE;
    }
}
